package com.zxl.register.server;


import com.zxl.register.server.config.RegisterServerConfig;
import com.zxl.register.server.event.EventProcess;
import com.zxl.register.server.schedule.ServerSchedule;
import com.zxl.register.server.socket.AcceptCompletionHandler;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.util.concurrent.ExecutionException;

public class RegisterServer {


    // 静态初始化 eventProcess
    private  EventProcess eventProcess=new EventProcess();

    public  AsynchronousServerSocketChannel serverSocketChannel=null;

    public AsynchronousServerSocketChannel getSocketChannel(){
        return serverSocketChannel;
    }

    RegisterServer() throws InterruptedException, ExecutionException, IOException {
        start();
    }

    public static void main(String[] args) throws InterruptedException, IOException, ExecutionException {
        new RegisterServer();

    }

    public  void start() throws ExecutionException, InterruptedException, IOException {
        // 创建一个对象
        serverSocketChannel = AsynchronousServerSocketChannel.open();
        // 绑定端口
        serverSocketChannel.bind(new InetSocketAddress(RegisterServerConfig.getInstance().getPort()));
        System.out.println("---------  Register Server Started !  ---------  ");
        System.out.println("---------  Register Server bind port :  [" + RegisterServerConfig.getInstance().getPort() + "]  --------------");

        // 心跳检测
        ServerSchedule.checkClientHeartBeat();
        serverSocketChannel.accept(this,new AcceptCompletionHandler());
    }

}
